<%@ page language="java"%>
<html>
<head>
<script type="text/javascript" src="js/add.js"></script>
<script type="text/javascript">
	function toggle()
	{
		if(document.getElementById('url').value.length!=0)
		{
			document.getElementById('components').style.display = 'block';
			document.getElementById('fetch').disabled='true';

			if (window.XMLHttpRequest)
			  {// code for IE7+, Firefox, Chrome, Opera, Safari
			  xmlhttp=new XMLHttpRequest();
			  }
			else
			  {// code for IE6, IE5
			  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
			  }
			xmlhttp.onreadystatechange=function()
			  {
				if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
				  var x = xmlhttp.responseXML.getElementsByTagName("component");

				  var tbl = document.getElementById('datatable');
				  if(x.length>1)
				  for (var i=0;i<x.length;i++)
				  {
			            var table = document.getElementById('datatable');
			            var rowCount = table.rows.length;
			            var row = table.insertRow(rowCount);
			            var cell1 = row.insertCell(0);
			            var element1 = document.createElement("input");
			            element1.type = "checkbox";
			            cell1.appendChild(element1);
			           
					  
						rowCount= rowCount + 1;

			            var cell2 = row.insertCell(1);
						var element2 = document.createElement("input");
						element2.type = "text";
						element2.value = x[i].getElementsByTagName("ComponentName")[0].childNodes[0].nodeValue
			            cell2.appendChild(element2);

						var cell3 = row.insertCell(2);
						var element3 = document.createElement("select");
						element3.name='sel';
						element3.options[0] = new Option('TextField','TextField');
						element3.options[1] = new Option('Radio','Radio');
						element3.options[2] = new Option('CheckBox','CheckBox');
						element3.options[3] = new Option('SelectList','SelectList');
						element3.options[4] = new Option('Hidden','Hidden');
			            
						var c_type = x[i].getElementsByTagName("ComponentType")[0].childNodes[0].nodeValue;
						if(c_type == "TextField")
							element3.options[0] = Option('TextField','TextField','selected');
						else if(c_type == "Radio")
							element3.options[1] = Option('Radio','Radio','selected');
						else if(c_type == "CheckBox")
							element3.options[2] = Option('CheckBox','CheckBox','selected');
						else if(c_type == "SelectList")
							element3.options[3] = Option('SelectList','SelectList','selected');
						else if(c_type == "Hidden")
							element3.options[4] = Option('Hidden','Hidden','selected');
						
			            cell3.appendChild(element3);


			 
				  }
					}
				}
			var path = "FetchUIElements_XML?urlTest=" + document.getElementById('url').value; 
			xmlhttp.open("GET",path,true);
			xmlhttp.send();
		}

		
	}
</script>

<script type="text/javascript">
function make_cookies()
{
	var table = document.getElementById('datatable');
	var blank = false;
    var rowCount = table.rows.length;
    for(var i=0; i<rowCount; i++) 
    {
        
        var row = table.rows[i];
        if(row.cells[1].childNodes[0].value == "")
        {
            blank = true;
            alert('Table could not contain any Component with empty ComponentName');
            return false;
        }
    }
    if(blank == false)
    {
        for(var j=2; j<rowCount; j++)
        {
        	var row = table.rows[j];
        	var c_name = row.cells[1].childNodes[0].value;
        	var value = row.cells[2].childNodes[0].value;
			var expiredays = 1;
			var exdate=new Date();
			exdate.setDate(exdate.getDate()+expiredays);
			document.cookie=c_name+ "=" +escape(value)+
			((expiredays==null) ? "" : ";expires="+exdate.toUTCString());
        } 
        
    }
    return true;
}
</script>
</head>
<body>

URL:<input type="text" name="URL" id="url"/>
<input type="button" value="Fetch" id="fetch" onclick="toggle()"/>

<div id="components" style="display:none">
	<table border="0" width="350px">
		<tr>
		<td>
			<table id="datatable" width="350px" border="0" align="center">
				<tr>
				<th>&nbsp;</th>
				<th align="center">Component Name</th>
				<th align="center">Component Type</th>
				</tr>
				<tr>
				<td>&nbsp;</td>
				<td align="right"><input type="button" value="Add" onclick="addRow('datatable')"></td>
				<td align="left"><input type="button" value="Remove" onclick="deleteRow('datatable')"></td>
				</tr>
			</table>
		</td>
		</tr>
		<tr>
		<td align="right"><input type="button" value="Next>>" onclick="make_cookies()"/></td>
		</tr>
	</table>
</div>

</body>
</html>
